use std::collections::HashSet;

struct Solution {}

fn main() {}

impl Solution {
    pub fn is_happy(n: i32) -> bool {
        let mut set = HashSet::new();
        set.insert(n);
        let mut res = Self::get_next(n);
        while res != 1 {
            if set.contains(&res) {
                return false;
            }
            set.insert(res);
            res = Self::get_next(res);
        }
        return true;
    }

    pub fn get_next(n: i32) -> i32 {
        let mut s = n.to_string();
        let mut res = 0;
        s.chars().for_each(|c| {
            res += ((c as i32 - '0' as i32)).pow(2);
        });
        return res;
    }
}